﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IFServerDLL.Model;

namespace IFServerDLL
{
    public class GlobalStatic
    {
        public static T Clone<T>(T t)where T:class
        {
            T clone = Activator.CreateInstance(typeof(T)) as T;
            var infos = typeof(T).GetProperties().Where(p=>p.CanWrite);
            foreach (var info in infos)
            {
                info.SetValue(clone, info.GetValue(t, null), null);
            }
            return clone;
        }
        /// <summary>
        /// copy a to b
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        public static void Copy(object a, object b)
        {
            if (a == null || b == null)
                return;
            var infos = b.GetType().GetProperties().Where(p => p.CanWrite);
            foreach (var info in infos)
            {
                info.SetValue(b, info.GetValue(a, null), null);
            }
        }
        public static decimal GetCashRatio(string instrument)
        {
            return 0.12m;
        }
        public static decimal GetCommissRatio(string instrument)
        {
            return 0.000052m;
        }
        public static decimal GetCash(string instrument, int volume, decimal price)
        {
            return GetCashRatio (instrument)* MyVariety.GetVarietyByInstrument(instrument).Count * price * volume;
        }
        public static decimal GetCommiss(string instrument, int volume, decimal price)
        {
            return Math.Abs( GetCommissRatio(instrument) * MyVariety.GetVarietyByInstrument(instrument).Count * price * volume);
        }
        /// <summary>
        /// 生成自定义的时间索引，每秒分4档
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="instrument"></param>
        /// <returns></returns>
        public static int GetIntByTime(DateTime dt)
        {

            if (dt.CompareTo(dt.Date.Add(new TimeSpan(9, 15, 0))) < 0)
            {
                return -1;
            }
            else if (dt.CompareTo(dt.Date.Add(new TimeSpan(9, 15, 0))) >= 0 && dt.CompareTo(dt.Date.Add(new TimeSpan(11, 30, 0))) <= 0)
            {
                return (int)((dt - dt.Date.Add(new TimeSpan(9, 15, 0))).TotalSeconds) * 4;
            }
            else if (dt.CompareTo(dt.Date.Add(new TimeSpan(13, 0, 0))) >= 0 && dt.CompareTo(dt.Date.Add(new TimeSpan(15, 15, 0))) <= 0)
            {
                return 32404 + (int)((dt - dt.Date.Add(new TimeSpan(13, 0, 0))).TotalSeconds) * 4;
            }
            else
            {
                return 64808;
            }


        }
        /// <summary>
        /// 根据时间索引，生成市场时间
        /// </summary>
        /// <param name="t"></param>
        /// <param name="instrument"></param>
        /// <returns></returns>
        public static TimeSpan GetTimeByInt(int t)
        {

            if (t == -1)
            {
                return new TimeSpan(9, 14, 0);
            }

            else if (t >= 0 && t < 32404)
            {
                TimeSpan p = new TimeSpan(9, 15, 0);
                DateTime dt = DateTime.Now.Date.Add(p).AddSeconds(t / 4);
                return dt.TimeOfDay;
            }
            else if (t >= 32404 && t < 64808)
            {
                TimeSpan p = new TimeSpan(13, 0, 0);
                DateTime dt = DateTime.Now.Date.Add(p).AddSeconds((t - 32404) / 4);
                return dt.TimeOfDay;
            }
            else
            {
                return new TimeSpan(15, 15, 0);
            }

        }
    }
}
